﻿using Newtonsoft.Json;
using PhysicalExamination5.Interface.SystemSetting;
using PhysicalExamination5.Model.DataMaintenance;
using PhysicalExamination5.Model.PhysicalExamination;
using PhysicalExamination5.Model.SystemSetting;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace PhysicalExamination5.Service.SystemSetting
{
    public class SJueSeUser : IJueSeUser
    {
        private readonly ISqlSugarClient db;
        public SJueSeUser(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 查询角色信息
        /// </summary>
        /// <returns></returns>
        public string GetSelJueSeAll()
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                var JuSe = new SqlSugarRepository<xt_JueSe>(db).getAll().Select(t => new
                {
                    t.JueSeID,
                    t.JueSeMingCheng,
                    t.ShunXu
                }).OrderBy(t => t.ShunXu);

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = JuSe
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 根据角色ID查询用户和权限
        /// </summary>
        /// <param name="strJuseId"></param>
        /// <returns></returns>
        public string GetSelYongHu(string strJuseId)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【角色ID：{strJuseId}】");

                #region 声明变量

                object listYuanQu;
                List<QuanXianDataClass> listCaiDan = new List<QuanXianDataClass>();
                List<QuanXianDataClass> listShenQingDan = new List<QuanXianDataClass>();
                List<QuanXianDataClass> listBaoBiao = new List<QuanXianDataClass>();

                #endregion

                #region 用户

                DataTable xt_UserTable = new SqlSugarRepository<object>(db)
                    .getDataTableBySqlQuery($@"select A.JueSeMingCheng,B.* from xt_JueSe A with(nolock),xt_UserTable B with(nolock) where A.JueSeID = B.JueSeID and B.JueSeID = '{strJuseId}' order by ShunXu");

                #endregion

                #region 院区权限

                DataTable YuanQuQuanXian = new SqlSugarRepository<object>(db).getDataTableBySqlQuery($@"select B.YuanQuID,YuanQuMingCheng from qx_YuanQu A with(nolock),jcsj_YuanQu B with(nolock) 
where A.YuanQuId = B.YuanQuID and JueSeID = '{strJuseId}'");
                listYuanQu = YuanQuQuanXian.AsEnumerable().Select(t => new
                {
                    strCode = t.Field<int>("YuanQuID").ToString(),
                    strName = t.Field<string>("YuanQuMingCheng")
                }).ToList();

                #endregion

                #region 菜单权限

                List<XT_CaiDan_Rule> listXT_CaiDan_Rule = new SqlSugarRepository<XT_CaiDan_Rule>(db).getAll();
                DataTable CaiDanQuanXian = new SqlSugarRepository<object>(db).getDataTableBySqlQuery($@"select B.ShangJiRuleId,B.RuleId,B.Title from qx_CaiDan A with(nolock),XT_CaiDan_Rule B with(nolock)
where A.CaiDanID = B.RuleId and A.JueSeID = '{strJuseId}' order by RuleId");

                var CaiDans = CaiDanQuanXian.AsEnumerable().Select(t => new
                {
                    ShangJiRuleId = t.Field<int>("ShangJiRuleId"),
                    RuleId = t.Field<int>("RuleId"),
                    Title = t.Field<string>("Title")
                }).ToList();

                foreach (var MuLuId in CaiDans.Select(t => t.ShangJiRuleId).Distinct().ToList())
                {
                    QuanXianDataClass quanXianData = new QuanXianDataClass();
                    quanXianData.strCode = MuLuId.ToString();
                    quanXianData.strName = listXT_CaiDan_Rule.Where(t => t.RuleId == int.Parse(MuLuId.ToString())).Select(t => t.Title).FirstOrDefault();
                    List<ChildrenClass> listchildren = new List<ChildrenClass>();
                    foreach (var CaiDan in CaiDans.Where(t => t.ShangJiRuleId == MuLuId))
                    {
                        ChildrenClass children = new ChildrenClass();
                        children.strCode = CaiDan.RuleId.ToString();
                        children.strName = CaiDan.Title.ToString();
                        listchildren.Add(children);
                    }
                    quanXianData.children = listchildren;
                    listCaiDan.Add(quanXianData);
                }

                #endregion

                #region 申请单权限

                DataTable KeShiShenQingDan = new SqlSugarRepository<object>(db).getDataTableBySqlQuery($@"
select A.KeShiID,KeShiMingCheng,A.ShenQingDanID,ShenQingDanMingCheng from qx_ShenQingDan A with(nolock),jcsj_KeShi B with(nolock),jcsj_ShenQingDan C with(nolock)
where A.KeShiID = B.KeShiID and A.ShenQingDanID = C.ShenQingDanID and A.JueSeID = '{strJuseId}' order by B.ShunXu,C.ShunXu");

                var KeShis = KeShiShenQingDan.AsEnumerable().Select(t => new
                {
                    KeShiID = t.Field<string>("KeShiID"),
                    KeShiMingCheng = t.Field<string>("KeShiMingCheng")
                }).Distinct().ToList();

                foreach (var KeShi in KeShis)
                {
                    QuanXianDataClass quanXianData = new QuanXianDataClass();
                    quanXianData.strCode = KeShi.KeShiID.ToString();
                    quanXianData.strName = KeShi.KeShiMingCheng.ToString();
                    var ShenQingDans = KeShiShenQingDan.AsEnumerable().Where(t => t.Field<string>("KeShiID") == KeShi.KeShiID)
                        .Select(t => new
                        {
                            ShenQingDanID = t.Field<string>("ShenQingDanID"),
                            ShenQingDanMingCheng = t.Field<string>("ShenQingDanMingCheng")
                        }).ToList();
                    List<ChildrenClass> listchildren = new List<ChildrenClass>();
                    foreach (var ShenQingDan in ShenQingDans)
                    {
                        ChildrenClass children = new ChildrenClass();
                        children.strCode = "_" + ShenQingDan.ShenQingDanID.ToString();
                        children.strName = ShenQingDan.ShenQingDanMingCheng.ToString();
                        listchildren.Add(children);
                    }
                    quanXianData.children = listchildren;
                    listShenQingDan.Add(quanXianData);
                }

                #endregion

                #region 报表权限

                List<bb_ZhuBaoBiao> listbb_ZhuBaoBiao = new SqlSugarRepository<bb_ZhuBaoBiao>(db).getAll();
                DataTable BaoBiaoQuanXian = new SqlSugarRepository<object>(db).getDataTableBySqlQuery($@"select FuBaoBiaoID,B.ZhuBaoBiaoID,B.BaoBiaoMingCheng from qx_BaoBiao A with(nolock),bb_ZhuBaoBiao B with(nolock) 
where A.ZhuBaoBiaoID = B.ZhuBaoBiaoID and JueSeID = '{strJuseId}' order by ZhuBaoBiaoID");
                var BaoBiaos = BaoBiaoQuanXian.AsEnumerable().Select(t => new
                {
                    FuBaoBiaoID = t.Field<string>("FuBaoBiaoID"),
                    ZhuBaoBiaoID = t.Field<string>("ZhuBaoBiaoID"),
                    BaoBiaoMingCheng = t.Field<string>("BaoBiaoMingCheng")
                }).ToList();
                foreach (var BaoBiaoId in BaoBiaos.Select(t => t.FuBaoBiaoID).Distinct().ToList())
                {
                    QuanXianDataClass quanXianData = new QuanXianDataClass();
                    quanXianData.strCode = BaoBiaoId.ToString();
                    quanXianData.strName = listbb_ZhuBaoBiao.Where(t => t.ZhuBaoBiaoID == BaoBiaoId.ToString()).Select(t => t.BaoBiaoMingCheng).FirstOrDefault();
                    List<ChildrenClass> listchildren = new List<ChildrenClass>();
                    foreach (var BaoBiao in BaoBiaos.Where(t => t.FuBaoBiaoID == BaoBiaoId))
                    {
                        ChildrenClass children = new ChildrenClass();
                        children.strCode = BaoBiao.ZhuBaoBiaoID.ToString();
                        children.strName = BaoBiao.BaoBiaoMingCheng.ToString();
                        listchildren.Add(children);
                    }
                    quanXianData.children = listchildren;
                    listBaoBiao.Add(quanXianData);
                }

            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = new
                    {
                        xt_UserTable,
                        listYuanQu,
                        listCaiDan,
                        listShenQingDan,
                        listBaoBiao
                    }
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 查询全部权限
        /// </summary>
        /// <returns></returns>
        public string GetSelQuanXian(string strYuanQu)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 声明变量

                List<jcsj_YuanQu> listjcsj_YuanQu;
                object listYuanQuClass;
                List<QuanXianDataClass> listCaiDanQuanXianDataClass = new List<QuanXianDataClass>();
                List<QuanXianDataClass> listShenQingDanQuanXianDataClass = new List<QuanXianDataClass>();
                List<QuanXianDataClass> listBaoBiaoQuanXianDataClass = new List<QuanXianDataClass>();

                #endregion

                #region 查询院区权限

                listjcsj_YuanQu = new SqlSugarRepository<jcsj_YuanQu>(db).getAll().OrderBy(t => t.ShunXu).ToList();
                listYuanQuClass = listjcsj_YuanQu.Select(t => new
                {
                    strCode = t.YuanQuID.ToString(),
                    strName = t.YuanQuMingCheng
                }).ToList();

                #endregion

                #region 查询菜单权限

                List<XT_CaiDan_Rule> listCaiDanMuLu = new SqlSugarRepository<XT_CaiDan_Rule>(db).getByWhere(t => t.Type == "菜单目录");
                foreach (var CaiDanMuLu in listCaiDanMuLu)
                {
                    QuanXianDataClass quanXianDataClass = new QuanXianDataClass();
                    quanXianDataClass.strCode = CaiDanMuLu.RuleId.ToString();
                    quanXianDataClass.strName = CaiDanMuLu.Title.ToString();
                    List<XT_CaiDan_Rule> listCaiDanItem = new SqlSugarRepository<XT_CaiDan_Rule>(db).getByWhere(t => t.ShangJiRuleId == CaiDanMuLu.RuleId);
                    List<ChildrenClass> listchildren = new List<ChildrenClass>();
                    foreach (var CaiDanItem in listCaiDanItem)
                    {
                        ChildrenClass children = new ChildrenClass();
                        children.strCode = CaiDanItem.RuleId.ToString();
                        children.strName = CaiDanItem.Title.ToString();
                        listchildren.Add(children);
                    }
                    quanXianDataClass.children = listchildren;
                    listCaiDanQuanXianDataClass.Add(quanXianDataClass);
                }

                #endregion

                #region 查询申请单权限

                List<jcsj_KeShi> listjcsj_KeShi = new SqlSugarRepository<jcsj_KeShi>(db).getByWhere(t => t.SuoShuYuanQu.Contains(strYuanQu)).OrderBy(t => t.ShunXu).ToList();
                foreach (var jcsj_KeShi in listjcsj_KeShi)
                {
                    QuanXianDataClass quanXianDataClass = new QuanXianDataClass();
                    quanXianDataClass.strCode = jcsj_KeShi.KeShiID.ToString();
                    quanXianDataClass.strName = jcsj_KeShi.KeShiMingCheng.ToString();
                    List<jcsj_ShenQingDan> listjcsj_ShenQingDan = new SqlSugarRepository<jcsj_ShenQingDan>(db).getByWhere(t => t.KeShiID == jcsj_KeShi.KeShiID && t.SuoShuYuanQu.Contains(strYuanQu))
                        .OrderBy(t => t.ShunXu).ToList();
                    List<ChildrenClass> listchildren = new List<ChildrenClass>();
                    foreach (var jcsj_ShenQingDan in listjcsj_ShenQingDan)
                    {
                        ChildrenClass children = new ChildrenClass();
                        children.strCode = "_" + jcsj_ShenQingDan.ShenQingDanID.ToString();
                        children.strName = jcsj_ShenQingDan.ShenQingDanMingCheng.ToString();
                        listchildren.Add(children);
                    }
                    if (listchildren.Count != 0)
                    {
                        quanXianDataClass.children = listchildren;
                        listShenQingDanQuanXianDataClass.Add(quanXianDataClass);
                    }
                }

                #endregion

                #region 查询报表权限

                List<bb_ZhuBaoBiao> listZhuBaoBiao = new SqlSugarRepository<bb_ZhuBaoBiao>(db).getByWhere(t => t.BaoBiaoMingCheng != "固定报表" && t.CengJi == 1).OrderBy(t => t.ShunXu).ToList();
                foreach (var ZhuBaoBiao in listZhuBaoBiao)
                {
                    QuanXianDataClass quanXianDataClass = new QuanXianDataClass();
                    quanXianDataClass.strCode = ZhuBaoBiao.ZhuBaoBiaoID.ToString();
                    quanXianDataClass.strName = ZhuBaoBiao.BaoBiaoMingCheng.ToString();
                    List<bb_ZhuBaoBiao> listZiBaoBiao = new SqlSugarRepository<bb_ZhuBaoBiao>(db).getByWhere(t => t.CengJi == 2 && t.FuBaoBiaoID == ZhuBaoBiao.ZhuBaoBiaoID).OrderBy(t => t.ShunXu).ToList();
                    List<ChildrenClass> listchildren = new List<ChildrenClass>();
                    foreach (var ZiBaoBiao in listZiBaoBiao)
                    {
                        ChildrenClass children = new ChildrenClass();
                        children.strCode = ZiBaoBiao.ZhuBaoBiaoID.ToString();
                        children.strName = ZiBaoBiao.BaoBiaoMingCheng.ToString();
                        listchildren.Add(children);
                    }
                    quanXianDataClass.children = listchildren;
                    listBaoBiaoQuanXianDataClass.Add(quanXianDataClass);
                }

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = new
                    {
                        listYuanQuClass,
                        listCaiDanQuanXianDataClass,
                        listShenQingDanQuanXianDataClass,
                        listBaoBiaoQuanXianDataClass
                    }
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 角色新增
        /// </summary>
        /// <param name="jueSe"></param>
        /// <returns></returns>
        public string PostJueSeAdd(JueSeClass jueSe, string strYuanQuQianZhui, string strXiTongMingCheng)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{jueSe}、{strYuanQuQianZhui}、{strXiTongMingCheng}】");

                #region 声明变量

                int itid = 0;
                string strSql = "";
                object Data = null;

                #endregion

                #region 验证数据

                if (jueSe.Validate("Add", out strFanHuiZhi) == false) goto PROC_EXIT;
                if (new SqlSugarRepository<xt_JueSe>(db).getByWhere(t => t.JueSeMingCheng == jueSe.strJuSeName).Count > 0)
                {
                    nret = 1;
                    strFanHuiZhi = $@"角色【{jueSe.strJuSeName}】已存在！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 保存角色

                DbResult<bool> result = new SqlSugarRepository<jcsj_WeiHaiYinSuZuHeXiangMu>(db).UseTran(() =>
                {
                    var ShunXu = new SqlSugarRepository<xt_JueSe>(db).getAll();
                    int iShunXu;
                    if (ShunXu.Any())
                    {
                        iShunXu = ShunXu.Max(t => t.ShunXu) + 1;
                    }
                    else
                    {
                        iShunXu = 1;
                    }
                    xt_JueSe xt_JueSe = new xt_JueSe();
                    xt_JueSe.XiTongMingCheng = strXiTongMingCheng.Trim();
                    xt_JueSe.JueSeMingCheng = jueSe.strJuSeName.Trim();
                    xt_JueSe.ShunXu = iShunXu;
                    itid = new SqlSugarRepository<xt_JueSe>(db).AddReturnIdentity(xt_JueSe);
                    strSql = $"update xt_JueSe set JueSeID='{strYuanQuQianZhui + itid}' where tid='{itid}'";
                    if (new SqlSugarRepository<object>(db).ExecuteCommandSql(strSql) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = "保存角色失败！";
                        return;
                    }
                    Data = new
                    {
                        JueSeId = strYuanQuQianZhui + itid,
                        JueSeName = xt_JueSe.JueSeMingCheng
                    };
                },
                 (ex) =>
                 {
                     nret = 1000;
                     strFanHuiZhi = ex.Message;
                     Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:事务报错异常:【{ex.Message}】");
                 });

            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = Data
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="jueSe"></param>
        /// <returns></returns>
        public string PutJueSeUpdate(JueSeClass jueSe)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 验证数据

                if (jueSe.Validate("Update", out strFanHuiZhi) == false) goto PROC_EXIT;
                if (new SqlSugarRepository<xt_JueSe>(db).getByWhere(t => t.JueSeID == jueSe.strJuSeId).Count == 0)
                {
                    nret = 1;
                    strFanHuiZhi = $@"未查询到角色id为{jueSe.strJuSeId}的记录";
                    goto PROC_EXIT;
                }

                #endregion

                #region 修改角色

                string strSql = $"update xt_JueSe set JueSeMingCheng='{jueSe.strJuSeName.Trim()}' where JueSeID='{jueSe.strJuSeId}'";
                if (new SqlSugarRepository<object>(db).ExecuteCommandSql(strSql) == false)
                {
                    nret = 1000;
                    strFanHuiZhi = "修改角色失败！";
                    goto PROC_EXIT;
                }

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = new
                    {
                        JueSeId = jueSe.strJuSeId,
                        JueSeName = jueSe.strJuSeName
                    }
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="jueSe"></param>
        /// <returns></returns>
        public string DeleteJueSeDelete(JueSeClass jueSe)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 验证数据

                if (jueSe.Validate("Delete", out strFanHuiZhi) == false) goto PROC_EXIT;
                if (new SqlSugarRepository<xt_UserTable>(db).getByWhere(t => t.JueSeID == jueSe.strJuSeId).Count > 0)
                {
                    nret = 1;
                    strFanHuiZhi = $@"当前角色下存在用户不可删除！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 删除角色和对应的权限

                DbResult<bool> result = new SqlSugarRepository<jcsj_WeiHaiYinSuZuHeXiangMu>(db).UseTran(() =>
                {
                    if (new SqlSugarRepository<qx_YuanQu>(db).DeleteWhere(t => t.JueSeID == jueSe.strJuSeId) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = $@"删除失败！";
                        return;
                    }
                    if (new SqlSugarRepository<qx_CaiDan>(db).DeleteWhere(t => t.JueSeID == jueSe.strJuSeId) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = $@"删除失败！";
                        return;
                    }
                    if (new SqlSugarRepository<qx_ShenQingDan>(db).DeleteWhere(t => t.JueSeID == jueSe.strJuSeId) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = $@"删除失败！";
                        return;
                    }
                    if (new SqlSugarRepository<qx_BaoBiao>(db).DeleteWhere(t => t.JueSeID == jueSe.strJuSeId) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = $@"删除失败！";
                        return;
                    }
                    if (new SqlSugarRepository<xt_JueSe>(db).DeleteWhere(t => t.JueSeID == jueSe.strJuSeId) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = $@"删除失败！";
                        return;
                    }
                    if (new SqlSugarRepository<xt_JueSe>(db).Update(t => new xt_JueSe
                    {
                        ShunXu = t.ShunXu - 1
                    }, t => t.ShunXu > jueSe.iShunXu) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = $@"修改失败！";
                        return;
                    }
                },
                 (ex) =>
                 {
                     nret = 1000;
                     strFanHuiZhi = ex.Message;
                     Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:事务报错异常:【{ex.Message}】");
                 });

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 用户新增
        /// </summary>
        /// <param name="userClass"></param>
        /// <returns></returns>
        public string PostUserAdd(UserClass userClass, string strYuanQuQianZhui)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 声明变量

                int itid = 0;
                string strSql = "";
                var webPath = Directory.GetCurrentDirectory();

                #endregion

                #region 验证数据

                if (userClass.Validate("Add", out strFanHuiZhi) == false) goto PROC_EXIT;
                if (new SqlSugarRepository<xt_UserTable>(db).getByWhere(t => t.uCode == userClass.struCode).Count > 0)
                {
                    nret = 1;
                    strFanHuiZhi = $@"当前用户代码【{userClass.struCode}】已经存在！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 保存用户

                DbResult<bool> result = new SqlSugarRepository<jcsj_WeiHaiYinSuZuHeXiangMu>(db).UseTran(() =>
                {
                    var ShunXu = new SqlSugarRepository<xt_UserTable>(db).getByWhere(t => t.JueSeID == userClass.strJueSeId);
                    int iShunXu;
                    if (ShunXu.Any())
                    {
                        iShunXu = ShunXu.Max(t => t.ShunXu) + 1;
                    }
                    else
                    {
                        iShunXu = 1;
                    }
                    xt_UserTable xt_UserTable = new xt_UserTable();
                    xt_UserTable.JueSeID = userClass.strJueSeId.Trim();
                    xt_UserTable.uCode = userClass.struCode.Trim();
                    xt_UserTable.uName = userClass.struName.Trim();
                    xt_UserTable.uPY = userClass.struPYM.ToLower().Trim();
                    xt_UserTable.uPWD = new MethodHelper(db).GetMD5Hash("123456").Trim();
                    xt_UserTable.ShenFenZhengHao = userClass.strShenFenZhengHao.Trim();
                    xt_UserTable.ZhiYeLeiBie = userClass.strZhiYeLeiBie.Trim();
                    xt_UserTable.YaoShiZhiCheng = userClass.strYaoShiZhiCheng;
                    xt_UserTable.SuoShuKeShi = userClass.strSuoShuKeShi.Trim();
                    xt_UserTable.XueLi = userClass.strXueLi.Trim();
                    xt_UserTable.ShouJiHao = userClass.strShouJiHao.Trim();
                    xt_UserTable.BeiZhu = userClass.strBeiZhu.Trim();
                    xt_UserTable.ShiFouZongJianYiShi = userClass.strShiFouZongJianYiShi.Trim();
                    xt_UserTable.YongHuZuiDaZheKou = userClass.strYongHuZuiDaZheKou.Trim();
                    xt_UserTable.DianZiQianMing = userClass.strDianZiQianMing.Trim();
                    xt_UserTable.ShunXu = iShunXu;
                    itid = new SqlSugarRepository<xt_UserTable>(db).AddReturnIdentity(xt_UserTable);
                    if (new SqlSugarRepository<xt_UserTable>(db).Update(t => new xt_UserTable 
                    { 
                        UserID = strYuanQuQianZhui + itid
                    },t => t.tID == itid) ==false)
                    {
                        nret = 1000;
                        strFanHuiZhi = "保存用户失败！";
                        return;
                    }
                },
                 (ex) =>
                 {
                     nret = 1000;
                     strFanHuiZhi = ex.Message;
                     Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:事务报错异常:【{ex.Message}】");
                 });

            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="userClass"></param>
        /// <returns></returns>
        public string PutUserUpdate(UserClass userClass)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 验证数据

                if (userClass.Validate("Update", out strFanHuiZhi) == false) goto PROC_EXIT;
                if (new SqlSugarRepository<xt_UserTable>(db).getByWhere(t => t.UserID == userClass.strUserId).Count == 0)
                {
                    nret = 1;
                    strFanHuiZhi = $@"未查询到用户ID为【{userClass.strUserId}】的记录";
                    goto PROC_EXIT;
                }

                #endregion

                #region 修改用户

                if (new SqlSugarRepository<xt_UserTable>(db).Update(t => new xt_UserTable
                {
                    uCode = userClass.struCode,
                    uName = userClass.struName,
                    uPY = userClass.struPYM.ToLower(),
                    ShenFenZhengHao = userClass.strShenFenZhengHao,
                    ZhiYeLeiBie = userClass.strZhiYeLeiBie,
                    YaoShiZhiCheng = userClass.strYaoShiZhiCheng,
                    SuoShuKeShi = userClass.strSuoShuKeShi,
                    XueLi = userClass.strXueLi,
                    ShouJiHao = userClass.strShouJiHao,
                    BeiZhu = userClass.strBeiZhu,
                    ShiFouZongJianYiShi = userClass.strShiFouZongJianYiShi,
                    YongHuZuiDaZheKou = userClass.strYongHuZuiDaZheKou,
                    DianZiQianMing = userClass.strDianZiQianMing
                }, t => t.UserID == userClass.strUserId) == false)
                {
                    nret = 1000;
                    strFanHuiZhi = $@"修改失败！";
                    goto PROC_EXIT;
                }

            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <returns></returns>
        public string DeleteUserDelete(YongHuClass yongHu)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【uCode】");

                #region 删除用户和权限

                DbResult<bool> result = new SqlSugarRepository<jcsj_WeiHaiYinSuZuHeXiangMu>(db).UseTran(() =>
                {
                    if (new SqlSugarRepository<qx_YuanQu>(db).DeleteWhere(t => t.uCode == yongHu.struCode) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = $@"删除失败！";
                        return;
                    }
                    if (new SqlSugarRepository<qx_CaiDan>(db).DeleteWhere(t => t.uCode == yongHu.struCode) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = $@"删除失败！";
                        return;
                    }
                    if (new SqlSugarRepository<qx_ShenQingDan>(db).DeleteWhere(t => t.uCode == yongHu.struCode) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = $@"删除失败！";
                        return;
                    }
                    if (new SqlSugarRepository<qx_BaoBiao>(db).DeleteWhere(t => t.uCode == yongHu.struCode) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = $@"删除失败！";
                        return;
                    }
                    if (new SqlSugarRepository<xt_UserTable>(db).DeleteWhere(t => t.uCode == yongHu.struCode) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = $@"删除失败！";
                        return;
                    }
                    if (new SqlSugarRepository<xt_UserTable>(db).Update(t => new xt_UserTable
                    {
                        ShunXu = t.ShunXu - 1
                    }, t => t.ShunXu > yongHu.iShunXu && t.JueSeID == yongHu.strJuseId) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = $@"修改失败！";
                        return;
                    }
                },
                 (ex) =>
                 {
                     nret = 1000;
                     strFanHuiZhi = ex.Message;
                     Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:事务报错异常:【{ex.Message}】");
                 });

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 解除用户锁定
        /// </summary>
        /// <param name="uCode"></param>
        /// <returns></returns>
        public string PutJieChuSuoDing(YongHuClass yongHu)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 验证数据

                if (new SqlSugarRepository<xt_UserTable>(db).getByWhere(t => t.uCode == yongHu.struCode).Count == 0)
                {
                    nret = 1;
                    strFanHuiZhi = $@"未查询到用户代码为为【{yongHu.struCode}】的记录";
                    goto PROC_EXIT;
                }

                #endregion

                #region 修改数据库

                if (new SqlSugarRepository<xt_UserTable>(db).Update(t => new xt_UserTable
                {
                    CuoWuCiShu = 0,
                    ShiFouSuoDing = "否"
                }, t => t.uCode == yongHu.struCode) == false)
                {
                    nret = 1000;
                    strFanHuiZhi = "数据库操作失败!";
                    goto PROC_EXIT;
                }

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 搜索特定用户
        /// </summary>
        /// <param name="strUser"></param>
        /// <returns></returns>
        public string GetSelUser(string strUser)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                DataTable xt_UserTable = new SqlSugarRepository<object>(db)
                    .getDataTableBySqlQuery($@"select A.JueSeMingCheng,B.* from xt_JueSe A with(nolock),xt_UserTable B with(nolock) where A.JueSeID = B.JueSeID and (B.uCode = '{strUser}' or B.uName = '{strUser}' or B.uPY = '{strUser}')");

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = xt_UserTable
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 角色保存权限
        /// </summary>
        /// <returns></returns>
        public string PostSaveJuSeQuanXian(SaveJuSeQuanXianClass saveJuSeQuanXian)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 验证数据

                if (saveJuSeQuanXian.Validate(out strFanHuiZhi) == false) goto PROC_EXIT;

                #endregion

                #region 根据不同类型去存储数据

                switch (saveJuSeQuanXian.strType)
                {
                    case "院区权限":

                        if (new SqlSugarRepository<qx_YuanQu>(db).DeleteWhere(t => t.JueSeID == saveJuSeQuanXian.strJueSeId) == false)
                        {
                            nret = 1000;
                            strFanHuiZhi = "删除院区权限失败！";
                            goto PROC_EXIT;
                        }
                        List<qx_YuanQu> listqx_YuanQu = new List<qx_YuanQu>();
                        foreach (var item in saveJuSeQuanXian.listQuanXianId)
                        {
                            qx_YuanQu qx_YuanQu = new qx_YuanQu();
                            qx_YuanQu.LeiXing = "角色";
                            qx_YuanQu.JueSeID = saveJuSeQuanXian.strJueSeId;
                            qx_YuanQu.YuanQuId = item;
                            listqx_YuanQu.Add(qx_YuanQu);
                        }
                        if (new SqlSugarRepository<qx_YuanQu>(db).AddRange(listqx_YuanQu) == false)
                        {
                            nret = 1000;
                            strFanHuiZhi = "保存院区权限失败！";
                            goto PROC_EXIT;
                        }

                        break;
                    case "菜单权限":

                        if (new SqlSugarRepository<qx_CaiDan>(db).DeleteWhere(t => t.JueSeID == saveJuSeQuanXian.strJueSeId) == false)
                        {
                            nret = 1000;
                            strFanHuiZhi = "删除菜单权限失败！";
                            goto PROC_EXIT;
                        }
                        saveJuSeQuanXian.listQuanXianId = new SqlSugarRepository<XT_CaiDan_Rule>(db)
                            .getByWhere(t => t.Type == "菜单项" && saveJuSeQuanXian.listQuanXianId.Contains(t.RuleId.ToString()))
                            .Select(t => t.RuleId.ToString())
                            .ToList();
                        List<qx_CaiDan> listqx_CaiDan = new List<qx_CaiDan>();
                        foreach (var item in saveJuSeQuanXian.listQuanXianId)
                        {
                            qx_CaiDan qx_CaiDan = new qx_CaiDan();
                            qx_CaiDan.LeiXing = "角色";
                            qx_CaiDan.JueSeID = saveJuSeQuanXian.strJueSeId;
                            qx_CaiDan.CaiDanID = int.Parse(item);
                            listqx_CaiDan.Add(qx_CaiDan);
                        }
                        if (new SqlSugarRepository<qx_CaiDan>(db).AddRange(listqx_CaiDan) == false)
                        {
                            nret = 1000;
                            strFanHuiZhi = "保存菜单权限失败！";
                            goto PROC_EXIT;
                        }

                        break;
                    case "申请单权限":

                        if (new SqlSugarRepository<qx_ShenQingDan>(db).DeleteWhere(t => t.JueSeID == saveJuSeQuanXian.strJueSeId) == false)
                        {
                            nret = 1000;
                            strFanHuiZhi = "删除申请单权限失败！";
                            goto PROC_EXIT;
                        }
                        saveJuSeQuanXian.listQuanXianId = saveJuSeQuanXian.listQuanXianId.Where(t => t.Contains("_")).Select(t => t.Replace("_", "")).ToList();
                        var KeShiShenQingDan = new SqlSugarRepository<jcsj_ShenQingDan>(db)
                            .getByWhere(t => saveJuSeQuanXian.listQuanXianId.Contains(t.ShenQingDanID))
                            .Select(t => new
                            {
                                t.KeShiID,
                                t.ShenQingDanID
                            })
                            .Distinct();
                        List<qx_ShenQingDan> listqx_ShenQingDan = new List<qx_ShenQingDan>();
                        foreach (var item in saveJuSeQuanXian.listQuanXianId)
                        {
                            qx_ShenQingDan qx_ShenQingDan = new qx_ShenQingDan();
                            qx_ShenQingDan.LeiXing = "角色";
                            qx_ShenQingDan.JueSeID = saveJuSeQuanXian.strJueSeId;
                            qx_ShenQingDan.ShenQingDanID = item;
                            qx_ShenQingDan.KeShiID = KeShiShenQingDan.Where(t => t.ShenQingDanID == item).Select(t => t.KeShiID).FirstOrDefault();
                            listqx_ShenQingDan.Add(qx_ShenQingDan);
                        }
                        if (new SqlSugarRepository<qx_ShenQingDan>(db).AddRange(listqx_ShenQingDan) == false)
                        {
                            nret = 1000;
                            strFanHuiZhi = "保存菜单权限失败！";
                            goto PROC_EXIT;
                        }

                        break;
                    case "报表权限":

                        if (new SqlSugarRepository<qx_BaoBiao>(db).DeleteWhere(t => t.JueSeID == saveJuSeQuanXian.strJueSeId) == false)
                        {
                            nret = 1000;
                            strFanHuiZhi = "删除申请单权限失败！";
                            goto PROC_EXIT;
                        }
                        saveJuSeQuanXian.listQuanXianId = new SqlSugarRepository<bb_ZhuBaoBiao>(db)
                           .getByWhere(t => t.CengJi == 2 && saveJuSeQuanXian.listQuanXianId.Contains(t.ZhuBaoBiaoID))
                           .Select(t => t.ZhuBaoBiaoID.ToString())
                           .ToList();
                        List<qx_BaoBiao> listqx_BaoBiao = new List<qx_BaoBiao>();
                        foreach (var item in saveJuSeQuanXian.listQuanXianId)
                        {
                            qx_BaoBiao qx_BaoBiao = new qx_BaoBiao();
                            qx_BaoBiao.LeiXing = "角色";
                            qx_BaoBiao.JueSeID = saveJuSeQuanXian.strJueSeId;
                            qx_BaoBiao.ZhuBaoBiaoID = item;
                            listqx_BaoBiao.Add(qx_BaoBiao);
                        }
                        if (new SqlSugarRepository<qx_BaoBiao>(db).AddRange(listqx_BaoBiao) == false)
                        {
                            nret = 1000;
                            strFanHuiZhi = "保存菜单权限失败！";
                            goto PROC_EXIT;
                        }

                        break;
                    default:
                        nret = 1;
                        strFanHuiZhi = $@"类型【{saveJuSeQuanXian.strType}】不可用！";
                        goto PROC_EXIT;
                }

            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 用户批量权限调整
        /// </summary>
        /// <returns></returns>
        public string PostUserZhuiJiaQuanXian(UserQuanXianClass userQuanXian)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 根据不同类型去存储数据

                switch (userQuanXian.strType)
                {
                    case "院区权限":
                        if (new SqlSugarRepository<qx_YuanQu>(db).DeleteWhere(t => userQuanXian.listUcode.Contains(t.uCode)) == false)
                        {
                            nret = 1000;
                            strFanHuiZhi = "删除院区权限失败！";
                            goto PROC_EXIT;
                        }
                        if (userQuanXian.strLeiBie == "追加")
                        {
                            List<qx_YuanQu> listqx_YuanQu = new List<qx_YuanQu>();
                            foreach (var uCode in userQuanXian.listUcode)
                            {
                                foreach (var QuanXianId in userQuanXian.listQuanXianId)
                                {
                                    qx_YuanQu qx_YuanQu = new qx_YuanQu();
                                    qx_YuanQu.LeiXing = "用户";
                                    qx_YuanQu.uCode = uCode;
                                    qx_YuanQu.YuanQuId = QuanXianId;
                                    listqx_YuanQu.Add(qx_YuanQu);
                                }
                            }
                            if (new SqlSugarRepository<qx_YuanQu>(db).AddRange(listqx_YuanQu) == false)
                            {
                                nret = 1000;
                                strFanHuiZhi = "保存院区权限失败！";
                                goto PROC_EXIT;
                            }
                        }
                        break;
                    case "菜单权限":
                        if (new SqlSugarRepository<qx_CaiDan>(db).DeleteWhere(t => userQuanXian.listUcode.Contains(t.uCode)) == false)
                        {
                            nret = 1000;
                            strFanHuiZhi = "删除菜单权限失败！";
                            goto PROC_EXIT;
                        }
                        if (userQuanXian.strLeiBie == "追加")
                        {
                            List<qx_CaiDan> listqx_CaiDan = new List<qx_CaiDan>();
                            foreach (var uCode in userQuanXian.listUcode)
                            {
                                foreach (var QuanXianId in userQuanXian.listQuanXianId)
                                {
                                    qx_CaiDan qx_CaiDan = new qx_CaiDan();
                                    qx_CaiDan.LeiXing = "用户";
                                    qx_CaiDan.uCode = uCode;
                                    qx_CaiDan.CaiDanID = int.Parse(QuanXianId);
                                    listqx_CaiDan.Add(qx_CaiDan);
                                }
                            }
                            if (new SqlSugarRepository<qx_CaiDan>(db).AddRange(listqx_CaiDan) == false)
                            {
                                nret = 1000;
                                strFanHuiZhi = "保存菜单权限失败！";
                                goto PROC_EXIT;
                            }
                        }
                        break;
                    case "申请单权限":
                        if (new SqlSugarRepository<qx_ShenQingDan>(db).DeleteWhere(t => userQuanXian.listUcode.Contains(t.uCode)) == false)
                        {
                            nret = 1000;
                            strFanHuiZhi = "删除申请单权限失败！";
                            goto PROC_EXIT;
                        }
                        if (userQuanXian.strLeiBie == "追加")
                        {
                            userQuanXian.listQuanXianId = userQuanXian.listQuanXianId.Select(t => t.Replace("_", "")).ToList();
                            var KeShiShenQingDan = new SqlSugarRepository<jcsj_ShenQingDan>(db)
                                .getByWhere(t => userQuanXian.listQuanXianId.Contains(t.ShenQingDanID))
                                .Select(t => new
                                {
                                    t.KeShiID,
                                    t.ShenQingDanID
                                })
                                .Distinct();
                            List<qx_ShenQingDan> listqx_ShenQingDan = new List<qx_ShenQingDan>();
                            foreach (var uCode in userQuanXian.listUcode)
                            {
                                foreach (var QuanXianId in userQuanXian.listQuanXianId)
                                {
                                    qx_ShenQingDan qx_ShenQingDan = new qx_ShenQingDan();
                                    qx_ShenQingDan.LeiXing = "用户";
                                    qx_ShenQingDan.uCode = uCode;
                                    qx_ShenQingDan.ShenQingDanID = QuanXianId;
                                    qx_ShenQingDan.KeShiID = KeShiShenQingDan.Where(t => t.ShenQingDanID == QuanXianId).Select(t => t.KeShiID).FirstOrDefault();
                                    listqx_ShenQingDan.Add(qx_ShenQingDan);
                                }
                            }
                            if (new SqlSugarRepository<qx_ShenQingDan>(db).AddRange(listqx_ShenQingDan) == false)
                            {
                                nret = 1000;
                                strFanHuiZhi = "保存菜单权限失败！";
                                goto PROC_EXIT;
                            }
                        }
                        break;
                    case "报表权限":
                        if (new SqlSugarRepository<qx_BaoBiao>(db).DeleteWhere(t => userQuanXian.listUcode.Contains(t.uCode)) == false)
                        {
                            nret = 1000;
                            strFanHuiZhi = "删除申请单权限失败！";
                            goto PROC_EXIT;
                        }
                        if (userQuanXian.strLeiBie == "追加")
                        {
                            List<qx_BaoBiao> listqx_BaoBiao = new List<qx_BaoBiao>();
                            foreach (var uCode in userQuanXian.listUcode)
                            {
                                foreach (var QuanXianId in userQuanXian.listQuanXianId)
                                {
                                    qx_BaoBiao qx_BaoBiao = new qx_BaoBiao();
                                    qx_BaoBiao.LeiXing = "用户";
                                    qx_BaoBiao.uCode = uCode;
                                    qx_BaoBiao.ZhuBaoBiaoID = QuanXianId;
                                    listqx_BaoBiao.Add(qx_BaoBiao);
                                }
                            }
                            if (new SqlSugarRepository<qx_BaoBiao>(db).AddRange(listqx_BaoBiao) == false)
                            {
                                nret = 1000;
                                strFanHuiZhi = "保存菜单权限失败！";
                                goto PROC_EXIT;
                            }
                        }
                        break;
                    default:
                        nret = 1;
                        strFanHuiZhi = $@"类型【{userQuanXian.strType}】不可用！";
                        goto PROC_EXIT;
                }

            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 用户角色顺序调整
        /// </summary>
        /// <param name="userJueSeShunXu"></param>
        /// <returns></returns>
        public string PutUserJueSeTiaoZhengShunXu(UserJueSeShunXuClass userJueSeShunXu)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                if (userJueSeShunXu.strType == "角色")
                {
                    if (new SqlSugarRepository<xt_JueSe>(db).Update(t => new xt_JueSe
                    {
                        ShunXu = userJueSeShunXu.iTiaoZhengHouShunXu
                    }, t => t.JueSeID == userJueSeShunXu.strJuseId) == false)
                    {
                        nret = 1;
                        strFanHuiZhi = "修改顺序失败，请检查！";
                        goto PROC_EXIT;
                    }

                    Expression<Func<xt_JueSe, xt_JueSe>> columns;
                    Expression<Func<xt_JueSe, bool>> expression;

                    if (userJueSeShunXu.iTiaoZhengQianShunXu < userJueSeShunXu.iTiaoZhengHouShunXu)
                    {
                        columns = t => new xt_JueSe
                        {
                            ShunXu = t.ShunXu - 1,
                        };
                        expression = t => t.ShunXu >= userJueSeShunXu.iTiaoZhengQianShunXu && t.ShunXu <= userJueSeShunXu.iTiaoZhengHouShunXu && t.JueSeID != userJueSeShunXu.strJuseId;
                    }
                    else if (userJueSeShunXu.iTiaoZhengQianShunXu > userJueSeShunXu.iTiaoZhengHouShunXu)
                    {
                        columns = t => new xt_JueSe
                        {
                            ShunXu = t.ShunXu + 1,
                        };
                        expression = t => t.ShunXu <= userJueSeShunXu.iTiaoZhengQianShunXu && t.ShunXu >= userJueSeShunXu.iTiaoZhengHouShunXu && t.JueSeID != userJueSeShunXu.strJuseId;
                    }
                    else
                    {
                        nret = 1;
                        strFanHuiZhi = "调整前后顺序一致！";
                        goto PROC_EXIT;
                    }

                    if (new SqlSugarRepository<xt_JueSe>(db).Update(columns, expression) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = "修改角色顺序失败，请检查！";
                        goto PROC_EXIT;
                    }
                }
                else if (userJueSeShunXu.strType == "用户")
                {
                    if (new SqlSugarRepository<xt_UserTable>(db).Update(t => new xt_UserTable
                    {
                        ShunXu = userJueSeShunXu.iTiaoZhengHouShunXu
                    }, t => t.UserID == userJueSeShunXu.strUserId) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = "修改用户顺序失败！";
                        goto PROC_EXIT;
                    }
                    Expression<Func<xt_UserTable, xt_UserTable>> columns;
                    Expression<Func<xt_UserTable, bool>> expression;
                    if (userJueSeShunXu.iTiaoZhengQianShunXu < userJueSeShunXu.iTiaoZhengHouShunXu)
                    {
                        columns = t => new xt_UserTable
                        {
                            ShunXu = t.ShunXu - 1
                        };
                        expression = t => t.ShunXu >= userJueSeShunXu.iTiaoZhengQianShunXu && t.ShunXu <= userJueSeShunXu.iTiaoZhengHouShunXu && t.UserID != userJueSeShunXu.strUserId && t.JueSeID == userJueSeShunXu.strJuseId;
                    }
                    else if (userJueSeShunXu.iTiaoZhengQianShunXu > userJueSeShunXu.iTiaoZhengHouShunXu)
                    {
                        columns = t => new xt_UserTable
                        {
                            ShunXu = t.ShunXu + 1
                        };
                        expression = t => t.ShunXu <= userJueSeShunXu.iTiaoZhengQianShunXu && t.ShunXu >= userJueSeShunXu.iTiaoZhengHouShunXu && t.UserID != userJueSeShunXu.strUserId && t.JueSeID == userJueSeShunXu.strJuseId;
                    }
                    else
                    {
                        nret = 1;
                        strFanHuiZhi = "调整前后顺序一致！";
                        goto PROC_EXIT;
                    }
                    if (new SqlSugarRepository<xt_UserTable>(db).Update(columns, expression) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = "修改角色顺序失败，请检查！";
                        goto PROC_EXIT;
                    }
                }
                else
                {
                    nret = 1;
                    strFanHuiZhi = $@"未发现类型为{userJueSeShunXu.strType}的处理！";
                    goto PROC_EXIT;
                }

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }

    }
}
